<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="labelRules" :model="labelForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="labelForm.title" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="labelForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="labelForm.status.value === 5" type="danger" @click="handleDelete(labelForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveLabel, removeLabelById, getLabel } from '@/api/customer/label'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      labelForm: {
        title: '',
        remark: '',
        status: { label: '', value: 1 }
      },
      labelRules: []
    }
  },
  created() {
    let labelId = this.$route.query && this.$route.query.labelId
    if (labelId !== undefined) {
      labelId = parseInt(labelId)
      if (!isNaN(labelId)) {
        this.listLoading = true
        const formData = {
          'labelId': labelId
        }
        getLabel(formData).then(response => {
          if (response.status === 200) {
            this.labelForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(labelId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'label' })
    },
    createData() {
      this.listLoading = true

      const _labelForm = this.labelForm
      try {
        _labelForm.status = _channelForm.status.value
        if(_labelForm.status == undefined){
          _labelForm.status = 1
        }
      } catch (error) {
        _labelForm.status = 1
      }
      if (_labelForm.identity > 0) {
        _labelForm.labelId = _labelForm.identity
      }
      saveLabel(_labelForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'label' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const formData = {
          'labelId': row.identity
        }
        removeLabelById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'group' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
